import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class MultipleChoiceQuestionView implements IView
{
	private JDialog dialog;
	private QuizController itsQuizController;
	private MultipleChoiceQuestion itsQuestion;
	private JRadioButton[] radioBtns;
	private NumberKeyListener itsNumberKeyListener;
	public JButton nextBtn;
	
	public MultipleChoiceQuestionView(MultipleChoiceQuestion question, QuizController ctrl)
	{
		itsQuizController = ctrl;
		itsQuestion = question;
		itsNumberKeyListener = new NumberKeyListener(this, ctrl);
	}

	@Override
	public void display(Window parent)
	{
		// dialog
		dialog = new JDialog(parent, itsQuestion.getTitle());
		dialog.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				itsQuizController.done();
			}
		});
		// buttons
		JButton prevBtn = new JButton("Previous");
		prevBtn.setPreferredSize(new Dimension(150, 80));
		prevBtn.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
		if(!showPrevButton())
		{
			prevBtn.setEnabled(false);
		}
        prevBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				itsQuizController.prev();
			}
		});
        prevBtn.addKeyListener(itsNumberKeyListener);
		
		nextBtn = new JButton("Next");
		nextBtn.setPreferredSize(new Dimension(150, 80));
		nextBtn.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
		if(itsQuizController.getCurrentResult().equals(""))
		{
			nextBtn.setEnabled(false);
		}
		nextBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dialog.setVisible(false);
				itsQuizController.next();
			}
		});
		nextBtn.addKeyListener(itsNumberKeyListener);
		// question text
		JLabel textLabel = new JLabel(itsQuestion.getText());
		FontManager.setComicFonts(textLabel);
		// radio buttons
		radioBtns = new JRadioButton[4];
		for(int i = 0; i < 4; ++i)
		{
			radioBtns[i] = new  JRadioButton(itsQuestion.getSelection(i));
			radioBtns[i].setFont(new Font("Comic Sans MS", Font.BOLD, 14));
			radioBtns[i].addActionListener(new SelectionButtonListener(itsQuizController, nextBtn, i));
			radioBtns[i].addKeyListener(itsNumberKeyListener);
		}
		String result = itsQuizController.getCurrentResult();
		if(!result.isEmpty())
		{
			radioBtns[Integer.parseInt(result)].setSelected(true);
		}
		// group the buttons together
		ButtonGroup group = new ButtonGroup();
		JPanel radioPanel = new JPanel(new GridLayout(4, 1));
		for(int i = 0; i < 4; ++i)
		{
			group.add(radioBtns[i]);
			radioPanel.add(radioBtns[i]);
			radioPanel.add(Box.createRigidArea(new Dimension(50, 0)));
			radioPanel.add(Box.createRigidArea(new Dimension(0, 50)));
		}
		
        JPanel centerPanel = new JPanel();
        centerPanel.add(radioPanel, BorderLayout.LINE_START);
        centerPanel.addKeyListener(itsNumberKeyListener);
        
        JLabel picture = new JLabel(createImageIcon(itsQuestion.getImageFile()));
        picture.addKeyListener(itsNumberKeyListener);
        centerPanel.add(picture, BorderLayout.CENTER);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        
		
		JPanel questionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		questionPanel.addKeyListener(itsNumberKeyListener);
		questionPanel.add(Box.createRigidArea(new Dimension(100, 0)));
		questionPanel.add(textLabel);
        dialog.getContentPane().add(questionPanel, BorderLayout.NORTH);
		dialog.getContentPane().add(centerPanel, BorderLayout.CENTER);

		// panels for buttons
		JPanel prevBtnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		prevBtnPanel.addKeyListener(itsNumberKeyListener);
		prevBtnPanel.add(prevBtn);
		
		JPanel nextBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		nextBtnPanel.addKeyListener(itsNumberKeyListener);
		nextBtnPanel.add(nextBtn);
		
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		btnPanel.addKeyListener(itsNumberKeyListener);
		btnPanel.add(prevBtnPanel);
		btnPanel.add(Box.createRigidArea(new Dimension(200, 0)));
		btnPanel.add(nextBtnPanel);
		dialog.getContentPane().add(btnPanel, BorderLayout.SOUTH);
		
		dialog.pack();
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - dialog.getWidth()) / 2;
		final int y = (screenSize.height - dialog.getHeight()) / 2;
		dialog.setLocation(x, y);
		
		dialog.setVisible(true);
	}
	
	private boolean showPrevButton()
	{
		return itsQuizController.getCurrentQuestion() > 0;
	}
	
	private ImageIcon createImageIcon(String filename)
	{
		return new ImageIcon(filename);
	}
	
	public void selectRadioBtn(int index)
	{
		if(index > -1 && index < 4)
		{
			for(int i = 0; i < 4; ++i)
			{
				radioBtns[i].setSelected(false);
			}
			radioBtns[index].setSelected(true);
		}
	}
}

class SelectionButtonListener implements ActionListener
{
	private QuizController itsQuizController;
	private JButton btn;
	private int answer;
	
	public SelectionButtonListener(QuizController ctrl, JButton btn, int answer)
	{
		this.itsQuizController = ctrl;
		this.answer = answer;
		this.btn = btn;
	}
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		new ClipPlayer(AudioFileManager.getAudioFile()).start();
		itsQuizController.setCurrentResult(Integer.toString(answer));
		btn.setEnabled(true);
	}
}


class NumberKeyListener implements KeyListener
{
	private MultipleChoiceQuestionView view;
	private QuizController itsQuizController;

	public NumberKeyListener(MultipleChoiceQuestionView view, QuizController ctrl)
	{
		this.view = view;
		this.itsQuizController = ctrl;
	}

	@Override
	public void keyPressed(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent evt)
	{
		new ClipPlayer(AudioFileManager.getAudioFile()).start();
		char c = evt.getKeyChar();
		int i = c;
		if(i >= 49 && i <= 52)
		{
			int index = i - 49;
			itsQuizController.setCurrentResult(Integer.toString(index));
			view.selectRadioBtn(index);
			view.nextBtn.setEnabled(true);
		}
	}
}

